//
//  CZInputTextView.m
//  globalTranslate
//
//  Created by 陈镇 on 2025/8/9.
//

#import "CZInputTextView.h"


@interface CZInputTextView ()<QMUITextViewDelegate>

@property(nonatomic,strong) QMUITextView *textField;
@property(nonatomic,strong) QMUIButton *sendBtn;
@property(nonatomic,strong) QMUIButton *inputBtn;
@end
@implementation CZInputTextView



-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
            
    
        
        self.backgroundColor = UIColor.whiteColor;
        
        [self addSubview:self.textField];
        [self addSubview:self.inputBtn];
        [self addSubview:self.sendBtn];
        
        
        
        
   
      
        
        
        UIBezierPath *shadowPath = [[UIBezierPath alloc] init];
        [shadowPath moveToPoint:CGPointMake(0, 0 )];
        [shadowPath addLineToPoint:CGPointMake(self.width, 0)];
        [shadowPath addLineToPoint:CGPointMake(self.width, -2)];
        [shadowPath addLineToPoint:CGPointMake(0, -2)];
        [shadowPath closePath];
 
        self.layer.shadowPath = shadowPath.CGPath;
        self.layer.shadowOffset = CGSizeMake(0, 1);
        self.layer.masksToBounds = NO;
        self.layer.shadowColor = UIColorHex(999999).CGColor;
        self.layer.shadowOpacity = 0.3;

    }
    return self;
}

-(QMUITextView *)textField{
    if (!_textField) {
        _textField = [[QMUITextView alloc] init];
        _textField.textColor = UIColorHex(222222);
        _textField.font = SLFontMakeRegular(18);
//        _textField.placeholder = @"输入或粘贴需要翻译的文字";
//        _textField.placeholderColor = UIColorHex(#9E9E9E);
        _textField.contentInset = UIEdgeInsetsMake(12, 12, 12, 12);
        _textField.backgroundColor = UIColorHex(#EBEDF5);
        _textField.delegate = self;
        [_textField sl_cornerRadius:5];
    }
    return _textField;
}

-(QMUIButton *)inputBtn{
    if (!_inputBtn) {
        _inputBtn = [QMUIButton createButtonTitle:@"输入或粘贴需要翻译的文字" titleColor:UIColorHex(#9E9E9E) font:SLFontMakeRegular(24)];
        [_inputBtn addTarget:self action:@selector(clickInput) forControlEvents:UIControlEventTouchUpInside];
    }
    return _inputBtn;
}
-(QMUIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [QMUIButton createButtonTitle:@"立即翻译" titleColor:UIColor.whiteColor font:SLFontMakeRegular(16)];
        [_sendBtn addTarget:self action:@selector(clickSend) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.backgroundColor = UIColorHex(#0D6886);
        [_sendBtn sl_cornerRadius:5];
    }
    return _sendBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _textField.frame = CGRectMake(10, 10, self.width - 20, self.height - 70);
    _sendBtn.frame = CGRectMake(10, _textField.bottom + 10, _textField.width, 40);

    _inputBtn.frame = _textField.frame;
}


-(void)clickSend{
    if (_textField.text.length>0) {
        if (_delegate && [_delegate respondsToSelector:@selector(inputTextViewReturn:)]) {
            [_delegate inputTextViewReturn:_textField.text];
            _textField.text = @"";
            _inputBtn.hidden = NO;
        }
    }
}

-(void)clickInput{
    _inputBtn.hidden = YES;
    [_textField becomeFirstResponder];
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
   
    if (_textField.text.length == 0) {
        _inputBtn.hidden = NO;
    }
    return YES;
}



@end
